package com.probox.modules.controller.platfom;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.ObjectUtil;
import com.probox.common.core.enums.DataStatus;
import com.probox.common.core.utils.SecurityUtils;
import com.probox.common.core.utils.poi.ExcelUtil;
import com.probox.common.core.web.controller.BaseController;
import com.probox.common.core.web.domain.AjaxResult;
import com.probox.common.core.web.page.TableDataInfo;
import com.probox.common.entity.domain.platfom.PlatformStoreGroup;
import com.probox.common.entity.mapper.system.SysUserMapper;
import com.probox.common.entity.service.platfom.IPlatformStoreGroupService;
import com.probox.common.log.annotation.Log;
import com.probox.common.log.enums.BusinessType;
import com.probox.common.security.annotation.PreAuthorize;
import com.probox.systemApi.domain.SysUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;


/**
 * 门店分组管理Controller
 *
 * @author usoft
 * @date 2022-09-17
 */
@RestController
@RequestMapping("/system/group")
public class PlatformStoreGroupController extends BaseController {
    @Autowired
    private IPlatformStoreGroupService platformStoreGroupService;

    @Autowired
    private SysUserMapper userMapper;



    @GetMapping("/listSelect")
    public AjaxResult listSelect() {
        PlatformStoreGroup platformStoreGroup = new PlatformStoreGroup();
        SysUser sysUser = userMapper.selectUserById(SecurityUtils.getUserId());
        if(ObjectUtil.isNotEmpty(sysUser.getStoreGroupId())){
            platformStoreGroup.setGroupId(sysUser.getStoreGroupId());
        }else if(ObjectUtil.isNotEmpty(sysUser.getStoreId())){
            return AjaxResult.success();
        }
        platformStoreGroup.setDataStatus(DataStatus.valid.getKey());
        return AjaxResult.success(platformStoreGroupService.queryList(platformStoreGroup));
    }

    @GetMapping("/listSelectSelf")
    public AjaxResult listSelectSelf() {

        return AjaxResult.success(platformStoreGroupService.selectPlatformStoreGroupById(userMapper.selectUserById(SecurityUtils.getUserId()).getStoreGroupId()));
    }

    /**
     * 查询门店分组管理列表
     */
    @GetMapping("/list")
    public TableDataInfo list(PlatformStoreGroup platformStoreGroup) {
        SysUser sysUser = userMapper.selectUserById(SecurityUtils.getUserId());
        if(ObjectUtil.isNotEmpty(sysUser.getStoreGroupId())){
            platformStoreGroup.setGroupId(sysUser.getStoreGroupId());
        }else if(ObjectUtil.isNotEmpty(sysUser.getStoreId())){
            return getDataTable(new ArrayList<>());
        }
        startPage();
        List<PlatformStoreGroup> list = platformStoreGroupService.queryList(platformStoreGroup);
        return getDataTable(list);
    }

    /**
     * 导出门店分组管理列表
     */
    @PostMapping("/export")
    public void export(HttpServletResponse response, PlatformStoreGroup platformStoreGroup) throws IOException {
        List<PlatformStoreGroup> list = platformStoreGroupService.queryList(platformStoreGroup);
        ExcelUtil<PlatformStoreGroup> util = new ExcelUtil<PlatformStoreGroup>(PlatformStoreGroup.class);
        util.exportExcel(response, list, "门店分组管理数据");
    }

    /**
     * 获取门店分组管理详细信息
     */
    @GetMapping(value = "/{groupId}")
    public AjaxResult getInfo(@PathVariable("groupId") Long groupId) {

        return AjaxResult.success(platformStoreGroupService.selectPlatformStoreGroupById(groupId));
    }

    /**
     * 新增门店分组管理
     */
    @PostMapping(value = "/add")
    public AjaxResult add(@RequestBody PlatformStoreGroup platformStoreGroup) {
        return toAjax(platformStoreGroupService.insertPlatformStoreGroup(platformStoreGroup));
    }

    /**
     * 修改门店分组管理
     */
    @PreAuthorize(hasPermi = "system:group:edit")
    @PutMapping(value = "/edit")
    public AjaxResult edit(@RequestBody PlatformStoreGroup platformStoreGroup) {
        return toAjax(platformStoreGroupService.updatePlatformStoreGroup(platformStoreGroup));
    }

    /**
     * 修改商品设备状态
     */
    @PreAuthorize(hasPermi = "platform:brand:edit")
    @Log(title = "商品设备", businessType = BusinessType.UPDATE)
    @PutMapping("/updateBrandStatus")
    public AjaxResult changeStatus(@RequestBody PlatformStoreGroup platformStoreGroup) {
        return toAjax(platformStoreGroupService.updatePlatformStoreGroup(platformStoreGroup));
    }

    /**
     * 删除门店分组管理
     */
    @DeleteMapping("/{groupIds}")
    public AjaxResult remove(@PathVariable Long[] groupIds) {
        return toAjax(platformStoreGroupService.deletePlatformStoreGroupByIds(groupIds));
    }

    /**
     * 根据个人权限，查看可视的分组
     * @return
     */
    @GetMapping("/groupList")
    public AjaxResult groupList() {
        PlatformStoreGroup platformStoreGroup = new PlatformStoreGroup();
        SysUser sysUser = userMapper.selectUserById(SecurityUtils.getUserId());
        if(ObjectUtil.isNotEmpty(sysUser.getStoreGroupId())){
            platformStoreGroup.setGroupId(sysUser.getStoreGroupId());
        }else if(ObjectUtil.isNotEmpty(sysUser.getStoreId())){
            return AjaxResult.success(new ArrayList<>());
        }
        return AjaxResult.success(platformStoreGroupService.queryList(platformStoreGroup));
    }
}
